import re,sys,os,socket
from urllib2 import *
from threading import Thread
from Queue import Queue
import time,gzip,tempfile,string,random
from handler import handler

class hentairules_handler(handler):
	def __init__(self):
		handler.__init__(self)
	def extract_url_lst(self,url):	
		"""
			take in url of gallery page
			return the list of urls of each image page
		"""
		page=handler.try_to_read_url(self,url)
		if page==None:return []
		return re.findall(r"href=\"[^<>\"]*redir=([^<>\"]*)\"",page)
	def download_img(self,url,file_name):
		"""
			take in url of image page, image file name(path)
			download the image with name file_name
		"""
		p=handler.try_to_read_url(self,url,5)
		if p==None:return
		lst=re.findall("[sS][rR][cC]=\"([^\"]*%s)\"" % self.ext,p)
		if len(lst)==0:
			handler.write_log(self,"fail to download %s" % file_name)
			return
		addr=lst[0]
		if not addr.startswith("http://"):
			addr=url[:url.find("img.php")]+addr
		handler.download_img(self,addr,file_name)
